/*socket client Handler
Created on 2020年05月10日
@author: zxyong 13738196011
 */

package com.zxy.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Date;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.business.Analyse_Data;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class ClientHandler implements Runnable
{
	private BufferedReader	reader;
	@SuppressWarnings("unused")
	private SocketAddress	sadd	= null;

	public void run()
	{
		try
		{
			if(Com_Para.clientSocket == null)
			{
				Com_Para.clientSocket = new Socket(Com_Para.ServerIP,Com_Para.ServerPort);
				Analyse_Data ad = new Analyse_Data();
				ad.Send01(Com_Para.clientSocket);
			}
			Com_Para.clientSocket.isBound();
			Com_Para.clientSocket.setKeepAlive(true);
		}
		catch(Exception dd)
		{
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("初次连接服务端错误：" + dd.getMessage() + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
			try
			{
				uL.WriteLogSub("NetWork");
			}
			catch(IOException e)
			{
			}
			System.out.println("网络连接错误：" + dd.getMessage() + "***" + Com_Para.ServerIP + ":" + Com_Para.ServerPort);
		}
		InputStreamReader isReader = null;
		Date rightStart = new Date();
		while(true)
		{
			long temp = new Date().getTime() - rightStart.getTime();
			try
			{
				if(Com_Para.clientSocket != null && Com_Para.clientSocket.isConnected() && !Com_Para.clientSocket.isClosed())
				{
					Com_Para.htServSockets.put("clientSocket",Com_Para.clientSocket);
					isReader = new InputStreamReader(Com_Para.clientSocket.getInputStream(),Com_Para.U_CODE);
					reader = new BufferedReader(isReader);
					char[] Bytes = new char[Com_Para.clientSocket.getReceiveBufferSize()];
					int iLength = reader.read(Bytes,0,Bytes.length);
					if(iLength > 0)
					{
						String strIP = "0.0.0.0";
						rightStart = new Date();
						Analyse_Data ad = new Analyse_Data();
						ad.SubClientRecBytes(iLength,Bytes,Com_Para.clientSocket,false,strIP);
					}
					else if(iLength <= 0)
					{
						Analyse_Data ad = new Analyse_Data();
						try
						{
							ad.SendSocket(new StringBuilder("*"),Com_Para.clientSocket);
						}
						catch(Exception dd)
						{
							System.out.println("服务端已关闭链接");
							UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("往服务端发送心跳包服务端已关闭链接" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
							try
							{
								uL.WriteLogSub("NetWork");
							}
							catch(IOException e)
							{
							}
							Com_Para.clientSocket = null;
							Com_Para.htServSockets.remove("clientSocket");
						}
						rightStart = new Date();
					}
				}
				else if(temp / 1000 > 30)
				{
					try
					{
						if(Com_Para.clientSocket == null)
						{
							Com_Para.clientSocket = new Socket(Com_Para.ServerIP,Com_Para.ServerPort);
							Com_Para.clientSocket.isBound();
							Com_Para.clientSocket.setKeepAlive(true);
							UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("尝试再次连接服务端成功" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
							try
							{
								uL.WriteLogSub("NetWork");
							}
							catch(IOException e)
							{
							}
							System.out.println("尝试再次连接服务端成功：" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:sss"));
							Analyse_Data ad = new Analyse_Data();
							ad.Send01(Com_Para.clientSocket);
						}
					}
					catch(Exception dd)
					{
						System.out.println("尝试再次连接服务端失败：" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:sss"));
						UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("尝试再次连接服务端失败" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
						try
						{
							uL.WriteLogSub("NetWork");
						}
						catch(IOException e)
						{
						}
						Com_Para.clientSocket = null;
						Com_Para.htServSockets.remove("clientSocket");
					}
					rightStart = new Date();
				}
				else
				{
					Com_Para.htServSockets.remove("clientSocket");
					Com_Para.clientSocket = null;
				}
				Thread.sleep(10);
			}
			catch(Exception e)
			{
				if(temp / 1000 > 10)
				{
					try
					{
						Com_Para.clientSocket = null;
						Com_Para.clientSocket = new Socket(Com_Para.ServerIP,Com_Para.ServerPort);
						Com_Para.clientSocket.isBound();
						Com_Para.clientSocket.setKeepAlive(true);
						System.out.println("循环连接连接服务端成功：" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS"));
						UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("循环连接连接服务端成功" + e.getMessage() + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
						try
						{
							uL.WriteLogSub("NetWork");
						}
						catch(IOException en)
						{
						}
						Analyse_Data ad = new Analyse_Data();
						ad.Send01(Com_Para.clientSocket);
					}
					catch(Exception dd)
					{
						System.out.println("循环连接服务端失败：" + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS"));
						UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("循环连接服务端失败" + dd.getMessage() + Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss:SSS")));
						try
						{
							uL.WriteLogSub("NetWork");
						}
						catch(IOException es)
						{
						}
						Com_Para.clientSocket = null;
						Com_Para.htServSockets.remove("clientSocket");
					}
					rightStart = new Date();
				}
			}
		}
	}
}